package com.github.jspxnet.worktask.core.job;

import com.github.jspxnet.json.JSONObject;
import com.github.jspxnet.network.http.HttpClient;
import com.github.jspxnet.network.http.HttpClientFactory;
import com.github.jspxnet.util.StringMap;
import com.github.jspxnet.utils.BeanUtil;
import com.github.jspxnet.utils.StringUtil;
import com.github.jspxnet.worktask.model.enums.FlowNodeEnumType;
import com.github.jspxnet.worktask.model.enums.WorkTaskEnumType;
import com.github.jspxnet.worktask.table.ConfFlowNode;
import com.github.jspxnet.worktask.table.WorkTask;
import java.util.Date;
import java.util.List;


/**
 * http方式回调触发
 * confFlowJob.getCallMethod()  里边是url地址
 * confFlowJob.getScript() 里边是请求参数
 * 会自动识别是json 还是 form 提交post方式
 * 返回data 可以指定下一个节点,如果不指定将进入下边的所有节点
 */
public class HttpJobTask extends BaseTaskAction{

    @Override
    public boolean execute() throws Exception {

        String out = StringUtil.empty;
        String callMethod = confFlowJob.getCallMethod();
        if (!StringUtil.isNull(callMethod))
        {
            HttpClient httpClient = HttpClientFactory.createHttpClient(callMethod);
            String script = confFlowJob.getScript();
            if (StringUtil.isNull(script))
            {
                out = httpClient.post();
            } else
            if (StringUtil.isJsonObject(script))
            {
                JSONObject json = new JSONObject(script);
                out = httpClient.post(json);
            } else
            {
                StringMap<String,String> valueMap = new StringMap<>();
                valueMap.setLineSplit(StringUtil.CRLF);
                valueMap.setKeySplit(StringUtil.EQUAL);
                valueMap.setString(script);
                out = httpClient.post(valueMap);
            }
        }
        //
        boolean runNext = false;
        if (StringUtil.isJsonObject(out)&&out.contains("data"))
        {
            JSONObject json = new JSONObject(out);
            String nodeId = json.getString("data");
            currentWorkTask.setWorkTaskType(WorkTaskEnumType.SCRIPT.getValue());
            currentWorkTask.setFlowNodeType(FlowNodeEnumType.END.getValue());
            currentWorkTask.setUsers(workTaskParam.getUsers());
            runtimeServiceDAO.update(currentWorkTask,new String[]{"flowNodeType","workTaskType","users"});

            List<ConfFlowNode> nextNodeList = confWorkFlowDAO.getNextList(currentWorkTask.getFlowNo(), currentWorkTask.getNamespace(), currentWorkTask.getNodeId());
            for (ConfFlowNode confFlowNode:nextNodeList)
            {
                if (nodeId!=null&&nodeId.equals(confFlowNode.getNodeId()))
                {
                    //不做任何事情,直接到下一节点
                    WorkTask workTask = BeanUtil.copy(workTaskParam, WorkTask.class);
                    workTask.setNodeId(confFlowNode.getNodeId());
                    workTask.setCreateDate(new Date());
                    workTask.setUsers(workTaskParam.getUsers());
                    runtimeServiceDAO.save(workTask);
                    runNext = true;
                    //继续到指定的节点
                }
            }
        }
        //是否执行到一下一节点过
        return !runNext;
    }
}